package cn.toolck.my.spring.dao.impl;

import cn.toolck.my.spring.annotation.Autowired;
import cn.toolck.my.spring.annotation.Component;
import cn.toolck.my.spring.dao.AccountDao;
import cn.toolck.my.spring.pojo.Account;
import cn.toolck.my.spring.service.TransferService;
import cn.toolck.my.spring.utils.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author 应癫
 */
@Component("accountDao")
public class JdbcAccountDaoImpl implements AccountDao {
    @Autowired
    private TransferService transferService;
    @Autowired
    private ConnectionUtils connectionUtils;


    @Override
    public Account queryAccountByCardNo(String cardNo) throws Exception {
        Connection connection = connectionUtils.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("select * from account where cardNo = ?");
        preparedStatement.setObject(1, cardNo);
        ResultSet resultSet = preparedStatement.executeQuery();
        Account account = new Account();
        while (resultSet.next()) {
            account.setCardNo(resultSet.getString("cardNo"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getInt("money"));
        }
        resultSet.close();
        preparedStatement.close();
        return account;
    }

    @Override
    public int updateAccountByCardNo(Account account) throws Exception {
        Connection connection = connectionUtils.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("update account set money = ? where cardNo = ?");
        preparedStatement.setObject(1, account.getMoney());
        preparedStatement.setObject(2, account.getCardNo());
        int i = preparedStatement.executeUpdate();
        preparedStatement.close();
        return i;
    }

    public ConnectionUtils getConnectionUtils() {
        return connectionUtils;
    }

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    public TransferService getTransferService() {
        return transferService;
    }

    public void setTransferService(TransferService transferService) {
        this.transferService = transferService;
    }
}
